package com.qdu.study_app.Activity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.qdu.study_app.Adapter.CommentListAdapter;
import com.qdu.study_app.R;
import com.qdu.study_app.bean.comment;
import com.qdu.study_app.fragment.StudyBoradFragment;
import com.qdu.study_app.utils.MyToast;

import org.w3c.dom.Comment;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;


/*
* 显示具体消息的页面
* */
public class Activity_Message extends ListActivity {

    private TextView uid;
    private TextView tvMessage;
    private EditText etComment;
    private Button pubComment;

    private String user_id;
    private String msg_id;
    private String msg_title;
    private String msg_content;
    private String msg_create_time;
    private String msg_update_time;



    private CommentListAdapter commentListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__message);
        commentListAdapter = new CommentListAdapter(Activity_Message.this);
        setListAdapter(commentListAdapter);
        initData();
        queryComment();
    }

    private void initData(){
        tvMessage = findViewById(R.id.tvMessage);
        pubComment = findViewById(R.id.btn_send_comment);
        etComment = findViewById(R.id.etComment);
        uid = findViewById(R.id.tv_user_id);

        Intent data = getIntent();
        user_id = data.getStringExtra(StudyBoradFragment.MSG_USR_ID);//System.out.println(user_id+"userid1______________________");
        msg_id = data.getStringExtra(StudyBoradFragment.MSG_ID);
        msg_title = data.getStringExtra(StudyBoradFragment.MSG_TITLE);
        msg_content = data.getStringExtra(StudyBoradFragment.MSG_CONTENT);
        msg_create_time = data.getStringExtra(StudyBoradFragment.MSG_CREATE_TIME);
        msg_update_time = data.getStringExtra(StudyBoradFragment.MSG_UPDATE_TIME);

        tvMessage.setText(msg_content);
        uid.setText(BmobUser.getCurrentUser().getObjectId()+"说：");

        // 给发布评论按钮设置监听事件
        pubComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(etComment.getText() == null || etComment.getText().toString().equals("")){
                    MyToast.showToast(Activity_Message.this,"评论不能为空",Toast.LENGTH_LONG);
                    return;
                }

                comment c = new comment();
                c.setTo_uid(user_id);
              //  System.out.println(user_id+"userid2______________________");
                c.setFrom_uid(BmobUser.getCurrentUser().getObjectId());
                c.setTopic_id(msg_id);
                c.setContent(etComment.getText().toString().trim());
                c.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        Toast.makeText(Activity_Message.this,"评论成功",Toast.LENGTH_LONG);
                        queryComment();
                        etComment.setText("");
                    }
                });

            }
        });


    }

    public void queryComment(){
        commentListAdapter.clear();
        // 获取评论
        BmobQuery<comment> bmobQuery = new BmobQuery<>();
        // 条件查询
        bmobQuery.addWhereEqualTo("topic_id",msg_id);
        System.out.println(msg_id+"--------------------");
        bmobQuery.findObjects(new FindListener<comment>() {
            @Override
            public void done(List<comment> list, BmobException e) {
                if(e == null){
                    commentListAdapter.addAll(list);
                }else {
                    MyToast.showToast(Activity_Message.this,"查询失败"+e.getMessage()+"--"+msg_id, Toast.LENGTH_LONG);
                }
            }
        });
    }


}